//公司名称
var corpName,scenicName
//机构名称
var districtId
$(function(){

     getList()
     getProvince()
});
//数据列表
function getList(){
    //构建列表
	$('#rolelist').getList({
		"initData":{
            "corpName":corpName,
            "scenicName":scenicName,
            "districtId":districtId,
            "auditStatus": 1,
            "unitType":2,
            "num": 1,
            "size":10
		},
		"url":site.apiUrl+"/back/scenicManagement/selectScenicInfoViewByCondition",// 数据请求url
        "type":"POST",
        "checkname": "datachk", //选择器名称，为空不显示选择器
        "checkattr": [ //选择器上增加自定义属性，以便传递多个参数
            { "attr": "scenicId", "value": "{{scenicId}}" },
          ],
        "col":[  //数据列
            {"field":"organTypeDisplay","display":"分类"},
            {"field":"scenicName","display":"机构名称"},
            {"field":"corpName","display":"公司名称"},
            {"field":"districtName","display":"区域"},
            // {"field":"adminName","display":"机构管理员姓名"},
            {"field":"account","display":"机构账号"},
        //    {"field":"genderDisplay","display":"性别"},
            // {"field":"identityCard","display":"身份证号"},
            {"field":"password","display":"账号密码"},	
            {"field":"phone","display":"联系电话"},
            {"field":"auditStatusDisplay","display":"状态"},
            {"field":"applicationDateTime","display":"创建时间"},
		],
		"option":[ //操作按钮配置
            {"icon":"view","tip":"查看","url":"detial-mechanism.html?scenicId={{scenicId}}&auditStatus={{auditStatus}}"},  // 花括号内可以对应相关字段
			{"icon":"edit","tip":"编辑","url":"add-mechanism.html?scenicId={{scenicId}}&auditStatus={{auditStatus}}"},  // 花括号内可以对应相关字段
			{"icon":"del","tip":"删除","fun":"deleteRole('{{scenicId}}')"}
		]
	});
}

//删除
//批量删除
$("#batchDelBtn").click(function(){
    var c = "";
    $("input[name=datachk]:checked").each(function(){
        c += $(this).attr('scenicId') + ',';
    });
    if(c!= null && c!=''){
        deleteRole(c);
    }else{
        ly.toast("请选择删除项");
    }
    
});
function deleteRole(id){
	var deleteUserId = window.localStorage.getItem("backstage-access-id");
	var obj = {
		'ids':id,
	};
	var url = site.apiUrl+"/back/scenicManagement/deleteScenicInfoByIds";
	ly.confirm("你确定删除吗？",function(){
		request({
	        url: url,
	        data:obj,
	        type: 'POST',
	        ajaxform:true,
	        callback: function(r) {
				if(r.code=="rest.success"){
					ly.toast('删除成功');
					$("#rolelist").getList.refresh();//刷新当前分页
	        	}else{
					ly.toast(data.result);
	        	}
	        }
		});
	});
}

//批量导入

$("#getexcel").on("input", function(e) {
    //判断是不是要求的上传类型
    if(!/\.(xls|xlsx|XLS|XLSX)$/.test($(this).val())) {
        ly.toast("导入文件格式不正确");
        $(this).val("");
        return false;
    }
    
    var formData = new FormData();
    formData.append('file', e.target.files[0]);
    $.ajax({
        headers: {
            "access-token": localStorage.getItem("backstage-access-token")
        },
        url: site.gateUrl + '/back/scenicManagement/importScenicInfo?unitType=2',
        type: "post",
        data: formData,
        processData: false, // 告诉jQuery不要去处理发送的数据  
        contentType: false, // 告诉jQuery不要去设置Content-Type请求头  
        success: function(r) {
            if(r.code == "rest.success") {
                if(!r.result){
                    ly.alert('导入失败，可能是导入部门或工种不是系统已存在的,请检查',function(){
                    
                    });
                }else{
                    ly.toast('导入成功');
                    $("#userList").getList.refresh();//刷新当前分页
                }
            } else {
                ly.toast(r.desc);
            }
        },
        error: function(e) {
            ly.toast("服务出错");
        }

    });

});

//查询省份
function getProvince(){
	var urls =''+site.gateUrl+'/back/areaManagement/selectAreaProvince'
	request({
		url:urls ,
		data:{},
		ajaxform:true,
		type: 'post',
		callback: function(r) {
			if(r.code=="rest.success"){
				 var lists = r.result;
				 var htmls = '';
				 for (let i = 0; i < lists.length; i++) {
					htmls+='<option value="'+lists[i].id+'">'+lists[i].name+'</option>'
				 }
				 $('#province').append(htmls)
			}else{

			}
		}
	});
}
// 通过省份查询市
function getcity(){
	var id=$("#province").val()
	if(id){
        $('#city').html('')
        $('#city').append('<option value="">请选择</option>')
		var urls =''+site.gateUrl+'/back/areaManagement/selectAreaCityByProvinceId'
		request({
			url:urls ,
			data:{provinceId:id},
			ajaxform:true,
			type: 'post',
			callback: function(r) {
				if(r.code=="rest.success"){
					var lists = r.result;
					var htmls = '';
					for (let i = 0; i < lists.length; i++) {
						htmls+='<option value="'+lists[i].id+'">'+lists[i].name+'</option>'
					}
					$('#city').append(htmls)
				}else{

				}
			}
		});
	}
	
}
//通过市查询区县
function getArea(){
	var id=$("#city").val()
	if(id){
        $('#Area').html('')
        $('#Area').append('<option value="">请选择</option>')
		var urls =''+site.gateUrl+'/back/areaManagement/selectAreaDistrictByCityId'
		request({
			url:urls ,
			data:{cityId :id},
			ajaxform:true,
			type: 'post',
			callback: function(r) {
				if(r.code=="rest.success"){
					var lists = r.result;
					var htmls = '';
					for (let i = 0; i < lists.length; i++) {
						htmls+='<option value="'+lists[i].id+'">'+lists[i].name+'</option>'
					}
					$('#Area').append(htmls)
				}else{

				}
			}
		});
	}
}
//查询
$('#search').click(function(){
   districtId = $('#Area').val()
   scenicName= $('#scenicName').val()
   corpName = $('#corpName').val()
   getList()
})